package cn.calm.parameter.action.validator;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class ParameterValueTypeValidator extends FieldValidatorSupport {

	@Override
	public void validate(Object object) throws ValidationException {
		String fieldName[] = getFieldName().split(",");

		Object parameterValue = this.getFieldValue(fieldName[0], object);
		Object parameterType = this.getFieldValue(fieldName[1], object);
		if (parameterType != null && parameterValue != null) {
			String parameterValueStr = String.valueOf(parameterValue);
			if(parameterValueStr.trim().length()<=0){
				return ;
			}
			int parameterTypeInt = Integer.parseInt(String
					.valueOf(parameterType));
			switch (parameterTypeInt) {
			case 0:
				break;
			case 1:
				try {
					Integer.parseInt(parameterValueStr);
				} catch (NumberFormatException e) {
					addFieldError(fieldName[0], object);
				}
				break;
			case 2:
				try {
					Double.parseDouble(parameterValueStr);
				} catch (NumberFormatException e) {
					addFieldError(fieldName[0], object);
				}
				break;
			default:
				break;
			}
		}
	}

}
